using System;
using System.Collections.Generic;
using System.Linq;
using HtmlAgilityPack;

namespace GearUp.Crawler
{
    public class LorebookDocument
    {
        private HtmlDocument doc;

        public LorebookDocument(string sourceHtml)
        {
            doc = new HtmlDocument();
            doc.OptionUseIdAttribute = true;
            doc.LoadHtml(sourceHtml);
        }
        
        public IEnumerable<string> LinksInArticleBodyDiv()
        {
            var bodyNode =
                doc.DocumentNode.Descendants("div").FirstOrDefault(
                    n => n.Id == "article_body");
            var result = from node in bodyNode.Descendants()
                         where node.Name == "a" && node.Attributes.Contains("href")
                         select node.Attributes["href"].Value;
			return result.Where(link => !link.Contains("#")).AsEnumerable();
        }

        public HtmlNode OfficialLorebookEntry()
        {
            var officialEntry =
                doc.DocumentNode.Descendants("div").FirstOrDefault(
                    n => n.Attributes["id"] != null && n.Attributes["id"].Value == "lorebookNoedit");
            return officialEntry;
        }

        public HtmlNode EditLink()
        {
            var editLink =
                doc.DocumentNode.Descendants("a").FirstOrDefault(
                    n => n.Attributes["id"] != null && n.Attributes["id"].Value == "ca-edit");
            return editLink;
        }
    }
}